#!/bin/bash

set -e
sudo -v

# Install apt pkgs
sudo apt update
sudo apt install -y build-essential python3-pip wget curl patch jq


# Set pip mirror to speed up pip pkg downloading
PIP_CONF=$HOME/.pip/pip.conf
mkdir -p $HOME/.pip
if [ ! -f "$PIP_CONF" ]; then
    touch "$PIP_CONF"
    echo "[global]" >> "$PIP_CONF"
    echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple/" >> "$PIP_CONF"
    echo "[install]" >> "$PIP_CONF"
    echo "trusted-host=pypi.tuna.tsinghua.edu.cn" >> "$PIP_CONF"
fi

# Install pip3 pkgs
pip3 install -r ../code/IEdgeInsights/build/requirements.txt
if [ -f "../code/IEdgeInsights/build/provision/cert_requirements.txt" ]; then
    pip3 install -r ../code/IEdgeInsights/build/provision/cert_requirements.txt
fi

# Install docker & docker-compose
if [ -f "docker-ce_20.10.9_3-0_ubuntu-focal_amd64.deb" ];then
    rm -rf docker-ce_20.10.9_3-0_ubuntu-focal_amd64.deb
fi
wget https://gitee.com/open-edge-insights/eii-install-guide/attach_files/873577/download/docker-ce_20.10.9_3-0_ubuntu-focal_amd64.deb

if [ -f "docker-ce-cli_20.10.9_3-0_ubuntu-focal_amd64.deb" ];then
    rm -rf docker-ce-cli_20.10.9_3-0_ubuntu-focal_amd64.deb
fi
wget https://gitee.com/open-edge-insights/eii-install-guide/attach_files/873579/download/docker-ce-cli_20.10.9_3-0_ubuntu-focal_amd64.deb

if [ -f "containerd.io_1.4.11-1_amd64.deb" ];then
    rm -rf containerd.io_1.4.11-1_amd64.deb
fi
wget https://gitee.com/open-edge-insights/eii-install-guide/attach_files/873578/download/containerd.io_1.4.11-1_amd64.deb

if [ -f "docker-compose-Linux-x86_64-v1.29.0" ];then
    rm -rf docker-compose-Linux-x86_64-v1.29.0
fi
wget https://gitee.com/open-edge-insights/eii-install-guide/attach_files/873576/download/docker-compose-Linux-x86_64-v1.29.0

sudo dpkg -i docker-ce_*.deb docker-ce-cli_*.deb containerd.io_*.deb

# Making the docker-compose executable.
sudo cp docker-compose-Linux-x86_64-v1.29.0 /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose

# Add $USER to 'docker' group
GROUP_NAME="docker"
egrep "^$GROUP_NAME" /etc/group
if [ $? -ne 0 ]; then
    sudo groupadd $GROUP_NAME
fi
sudo usermod -aG $GROUP_NAME $USER
